<template>
	<view class="info-input">
		<view class="item">
			<view class="title">{{dataObj.title}}</view>
			<picker class="content" mode="selector" :range="dataObj.list" :value="dataObj.selectIndex" range-key="label" @change="selectChange">
				<text>{{dataObj.list[dataObj.selectIndex]['label']}}</text>
			</picker>
		</view>
		<view class="footer">
			<view class="btn" @click="onPostTap">确定</view>
		</view>
	</view>
</template>

<script>
	import { requestErrorTip, checkMobileReg, getInfoData } from '@/common/tools.js';
	
	export default {
		data() {
			return {
				dataObj: {
					value: '',
					type: '',
					title: '',
					list: [],
					selectIndex: 0
				},
				dicObj: {
					sex: [{
						label: '请选择',
						value: ''
					}, {
						label: '男',
						value: '0'
					}, {
						label: '女',
						value: '1'
					}],
					profession: [{
						label: '请选择',
						value: ''
					}, {
						label: '医生',
						value: '1'
					}, {
						label: '老是',
						value: '0'
					}],
					level: [{
						label: '请选择',
						value: ''
					}, {
						label: '一级',
						value: '1'
					}, {
						label: '二级',
						value: '0'
					}],
					stu_grade: [{
						label: '请选择',
						value: ''
					}, {
						label: '初中',
						value: '1'
					}, {
						label: '高中',
						value: '2'
					}, {
						label: '中专',
						value: '3'
					}, {
						label: '大专',
						value: '4'
					}, {
						label: '本科',
						value: '5'
					}, {
						label: '硕士',
						value: '6'
					}, {
						label: '博士',
						value: '7'
					}]
				}
			}
		},
		onLoad(event) {
			this.dataObj['value'] = event['value']; // 获取页面数据类型
			this.dataObj['type'] = event['type']; // 获取页面数据类型
			this.initPage();
		},
		methods: {
			// 初始化页面
			initPage() {
				const value = this.dataObj['value'];
				const type = this.dataObj['type'];
				let title = '';
				switch(type) {
					case 'sex':
						title = '性别';
						break;
					case 'profession':
						title = '行业类别';
						break;
					case 'level':
						title = '职业技能等级';
						break;
					case 'stu_grade':
						title = '文化程度';
						break;
					default:
						break;
				}
				this.dataObj['title'] = title;
				this.dataObj['list'] = this.dicObj[type];
				uni.setNavigationBarTitle({
				　　title: title
				});
				// 获取选中的下标值
				for(let index = 0; index < this.dicObj[type].length; index++) {
					if (value === this.dicObj[type][index]['value']) {
						this.dataObj['selectIndex'] = index;
						console.log(index, 'index');
						break;
					}
				}
			},
			// 监听选择框变化
			selectChange(res) {
				this.dataObj['selectIndex'] = res.detail.value;
			},
			// 确定提交
			onPostTap() {
				const pData = {};
				const index = this.dataObj['selectIndex'];
				const value = this.dataObj['list'][index]['value'];
				const type = this.dataObj['type'];
				// if(value === '') {
				// 	requestErrorTip('toast', '选择框不能为空');
				// 	return false;
				// }
				pData[type] = value;
				this.$Requset({
					url: this.$Api['user']['editinfo'],
					data: pData,
					success: (res) => {
						const { data, status, msg } = res;
						if (!status) {
							requestErrorTip('toast', msg);
							return;
						}
						// 重新获取并存储个人信息数据
						getInfoData(() => {
							uni.navigateBack();
						});
					},
					fail: (err) => {
						console.log(err);
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page{
		background-color: #f5f5f5;
	}
	.info-input{
		padding-top: 18rpx;
		.item{
			position: relative;
			height: 44rpx;
			padding: 30rpx 36rpx 30rpx 230rpx;
			background-color: #FFFFFF;
			.title{
				box-sizing: border-box;
				position: absolute;
				top: 30rpx;
				left: 0;
				width: 230rpx;
				height: 44rpx;
				line-height: 44rpx;
				font-size: 30rpx;
				color: #1A1A1A;
				padding-left: 36rpx;
			}
			.content{
				position: relative;
				height: 44rpx;
				line-height: 44rpx;
				font-size: 30rpx;
				color: #7F7F7F;
				text-align: right;
				vertical-align: top;
				padding-right: 40rpx;
				&::after{
					content: '';
					position: absolute;
					top: 7rpx;
					right: 0;
					width: 30rpx;
					height: 30rpx;
					background-position: center;
					background-repeat: no-repeat;
					background-size: 100% 100%;
					background-image: url(../../../static/images/common/arrow-right.png);
				}
			}
		}
		.footer{
			padding: 36rpx;
			.btn{
				width: 100%;
				height: 82rpx;
				line-height: 82rpx;
				font-size: 32rpx;
				color: #FFFFFF;
				text-align: center;
				background-color: $color-common;
				border-radius: 41rpx;
			}
		}
	}
</style>
